import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    currentMusicTitle: '', // 当前播放歌曲名
    currentMusicArtist: '', // 当前播放歌曲歌手名
    currentPlayProgress: 0, // 当前播放进度
    currentLyrics: {}, // 当前歌词
    currentSongImg: '', // 当前歌曲图片
    currentPlayMusicIndex: -1, // 当前播放歌曲索引
    playList: [], // 播放列表
    lovedList: [], // 收藏列表
    recordList: [], // 播放记录列表
    isPlaying: false, // 当前是否在播放
  },
  mutations: {
    // savePath(state, pathName) {
    //   state.pathName = pathName;
    // },

    // 更新歌曲名
    updateMusicTitle(state, title) {
      state.currentMusicTitle = title
    },

    // 更新歌手名
    updateMusicArtist(state, artist) {
      state.currentMusicArtist = artist
    },

    // 同步当前歌曲播放进度
    recordProgress(state, time) {
      state.currentPlayProgress = time
    },

    // 切换播放状态
    togglePlaying(state, isPlay) {
      state.isPlaying = isPlay
    },

    // 更新当前播放索引
    updatePlayIndex(state, index) {
      state.currentPlayMusicIndex = index
    },

    // 更新当前歌词
    updateLyrics(state, lyrics) {
      state.currentLyrics = lyrics
    },

    // 更新当前歌曲封面
    updateSongIMG(state, img) {
      state.currentSongImg = img
    },

    // 从缓存中存入播放列表
    savePlayList(state, list) {
      state.playList = list
    },

    // 添加到播放列表
    add2PlayList(state, item) {
      state.playList.push(item)
    },

    // 从播放列表当中删除单个
    deleteOneFromPlayList(state, index) {
      let a = state.currentPlayMusicIndex


      // 要删除歌曲索引在当前播放歌曲前时，播放索引减1
      if (index < a && a > 0) {
        state.currentPlayMusicIndex--
      }

      // 删除当前播放歌曲，进行封面和歌词同步
      if (index === a && a !== 0) {
        if (index !== state.playList.length - 1) { // 不是最后一首
          state.currentSongImg = state.playList[index].img
          state.currentLyrics = Object.assign({
            natLy: state.playList[index].lyrics
          })
        } else { // 是最后一首
          state.currentSongImg = state.playList[index - 1].img
          state.currentLyrics = Object.assign({
            natLy: state.playList[index - 1].lyrics
          })
          state.currentPlayMusicIndex--
        }
      }

      // 播放列表只剩一首
      if (state.playList.length === 1) {
        state.currentLyrics = ''
        state.currentSongImg = ''
        state.currentPlayMusicIndex = -1
      }

      state.playList.splice(index, 1)
      state.isPlaying = false
    },

    // 从播放列表中删除多个
    deleteMoreFromPlayList(state, selectList) {
      state.playList = state.playList.filter(v => {
        return !selectList.find(target => {
          return target.time === v.time
        })
      })
    },

    // 从缓存中存入收藏列表
    saveLovedList(state, list) {
      state.lovedList = list
    },

    // 添加到收藏列表
    add2LovedList(state, item) {
      state.lovedList.unshift(item)
    },

    // 从收藏列表中移除单个
    deleteOneFromLovedList(state, index) {
      let i = state.playList.findIndex(v => {
        return v.time === state.lovedList[index].time
      })
      if (i > -1)
        state.playList[i].loved = false
      state.lovedList.splice(index, 1)
    },

    // 从收藏列表中移除多个
    deleteMoreFromLovedList(state, selectList) {
      for (let item of state.playList) {
        if (selectList.some(v => {
            return v.time === item.time
          })) {
          item.loved = false
        }
      }
      state.lovedList = state.lovedList.filter(v => {
        return !selectList.find(target => {
          return target.time === v.time
        })
      })
    },

    // 从缓存中存入播放记录列表
    saveRecordList(state, list) {
      state.recordList = list
    },

    // 添加到播放记录列表，并添加最后播放时间
    add2RecordList(state, item) {
      let i = state.recordList.findIndex(v => {
          return v.title === item.title && v.artist === item.artist && v.id === item.id
        }),
        d = new Date(),
        y = d.getFullYear(),
        m = d.getMonth() + 1,
        da = d.getDate(),
        h = d.getHours(),
        minute = d.getMinutes()
      m = m < 10 ? ('0' + m) : m
      da = da < 10 ? ('0' + da) : da
      h = h < 10 ? ('0' + h) : h
      minute = minute < 10 ? ('0' + minute) : minute

      if (i !== -1) { // 播放记录已经有则删除，重新更新记录
        state.recordList.splice(i, 1)
      }
      item.lastPlayTime = h + ':' + minute + ' ' + m + '.' + da
      state.recordList.unshift(item)
    },

    // 从播放记录列表中删除单个
    deleteOneFromRecordList(state, index) {
      state.recordList.splice(index, 1)
    },

    // 从播放记录列表中删除多个
    deleteMoreFromRecordList(state, selectList) {
      state.recordList = state.recordList.filter(v => {
        return !selectList.find(target => {
          return target.time === v.time
        })
      })
    },
  },
  actions: {}
})
